package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ViewCtranapplyQueryCmd;
import com.jackrain.nea.fc.cp.mapper.ViewCtranapplyMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 店务考勤系统-营业员调店
 *
 * @author WSL
 * @create 2017/11/20
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "cp")
public class ViewCtranapplyQueryCmdImpl extends CommandAdapter implements ViewCtranapplyQueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        // 常量
        String salerId = "CP_C_SALER_ID";
        String storeId = "CP_C_STORE_ID";
        String destId = "CP_C_DEST_ID";
        String tranDate = "TRANDATE";

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCtranapplyQueryCmdImpl--param-->{}",param);

        if (null == param || param.size() == 0){
            throw new NDSException("参数格式错误！");
        }
        // Mapper
        ViewCtranapplyMapper viewCtranapplyMapper = ApplicationContextHandle.getBean(ViewCtranapplyMapper.class);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 根据param查询条件 查询【营业员调店】表
        List<HashMap> list = viewCtranapplyMapper.selectProvider(param.getJSONArray("IDS").toArray());
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap: list) {

            // 营业员编号
            if (null != hashMap.get(salerId)){
                String salerEcode = viewCtranapplyMapper.getSalerEcode(TypeUtils.castToLong(hashMap.get("CP_C_SALER_ID")));
                hashMap.put("CP_C_SALER_ID",salerEcode);
            }
            // 调出店仓
            if (null != hashMap.get(storeId)){
                String storeEcode = viewCtranapplyMapper.getStoreEcode(TypeUtils.castToLong(hashMap.get("CP_C_STORE_ID")));
                hashMap.put("CP_C_STORE_ID",storeEcode);
            }
            // 调入店仓
            if (null != hashMap.get(destId)){
                String destEcode = viewCtranapplyMapper.getStoreEcode(TypeUtils.castToLong(hashMap.get("CP_C_DEST_ID")));
                hashMap.put("CP_C_DEST_ID",destEcode);
            }
            // 调店日期
            if (null != hashMap.get(tranDate)){
                hashMap.put("TRANDATE", sdf.format(TypeUtils.castToDate(hashMap.get("TRANDATE").toString())));
            }else{
                throw new NDSException("调店日期不能为空！");
            }

            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);
        }
        vh.put("data",resultJson);
        return vh;
    }
}
